using System;
using UnityEngine;

namespace Game.Framework.UI
{
    public abstract class UIView : UIElement
    {
        protected virtual bool _fitIphoneX => false;
        protected virtual bool _playOpenSound => true;

        private static AudioClip _commonOpenSound;
        private Type _type;

        protected override void OnCreate()
        {
            base.OnCreate();
            _type = GetType();

            if (_fitIphoneX) FitIphoneX();

            BindRes(true);
        }

        protected internal override void OnOpen<M, T>(UIData data)
        {
            base.OnOpen<M, T>(data);
        }

        protected internal override void OnOpen<T>(UIData data)
        {
            base.OnOpen<T>(data);

            if (_playOpenSound) PlayOpenSound();

            EventBus.Register<EVENT_UI_CLOSE>(OnEventUIClose);
            EventBus.Register<EVENT_UI_CLOSE_ALL_VIEW>(OnEVENT_UI_CLOSE_ALL_VIEW);
        }

        protected override void OnClose()
        {
            base.OnClose();

            EventBus.UnRegister<EVENT_UI_CLOSE>(OnEventUIClose);
            EventBus.UnRegister<EVENT_UI_CLOSE_ALL_VIEW>(OnEVENT_UI_CLOSE_ALL_VIEW);
        }

        protected internal override void OnRemove()
        {
            base.OnRemove();

            BindRes(false);
        }

        protected virtual void PlayOpenSound()
        {
            // if (_commonOpenSound == null) _commonOpenSound = ResTool.Instance.Load<AudioClip>(SoundPath.Scm4_common_popup_open);
            // AudioTool.Instance.PlaySound(_commonOpenSound);
        }

        private void BindRes(bool bind)
        {
            // if (bind)
            // {
            //     ResTool.Instance.BindBundle(BundleDependences);
            //     ResTool.Instance.BindAtlas(AtlasDependences);
            // }
            // else
            // {
            //     ResTool.Instance.UnBindBundle(BundleDependences);
            //     ResTool.Instance.UnBindAtlas(AtlasDependences);
            // }
        }

        private void OnEventUIClose(EVENT_UI_CLOSE e)
        {
            if (e.t != _type) return;

            OnClose();
        }


        private void OnEVENT_UI_CLOSE_ALL_VIEW(EVENT_UI_CLOSE_ALL_VIEW obj)
        {
            Close();
        }

        public void Close()
        {
            // OnEventUIClose(new EVENT_UI_CLOSE(_type));
            EventBus.Send(new EVENT_UI_CLOSE(_type));
        }

        private void FitIphoneX()
        {
            var root = _transform as RectTransform;
            // var offsetMax = root!.offsetMax;
            // offsetMax.y += GetSafeAreaOffset();
            // root.offsetMax = offsetMax;

            var safeArea = Screen.safeArea;
            var screenHeight = Screen.height;
            var topOffset = screenHeight - safeArea.yMax - safeArea.yMin;
            var offsetMax = root.offsetMax;

            if (topOffset != 0)
            {
                offsetMax.y += -topOffset - 15; // offsetMax.y 应为负值
                root.offsetMax = offsetMax;
            }
        }

        public float GetSafeAreaOffset()
        {
            int safeAreaOffset = (int)(Screen.height - Screen.safeArea.yMax);
            if (safeAreaOffset == 0) return 0.0f;

            safeAreaOffset = safeAreaOffset / 2;
            float scaleRatio = UIManager.Instance._canvasScaler.referenceResolution.y / Screen.height;
            safeAreaOffset = (int)(safeAreaOffset * scaleRatio);
            safeAreaOffset += 5;
            return -safeAreaOffset;
        }
    }
}